using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CS6238Project2.Banking.Entity;

using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;

using System.IO;
using System.Diagnostics;


namespace CS6238Project2.GUI
{
    /// <summary>
    /// The purpose of this class is to allow one to open an old version
    /// of a user object or create a new one
    /// </summary>
    public partial class Initialization : Form
    {
        private object userObject;
        private string name;

        public Initialization()
        {
            InitializeComponent();
            /*openFileDialog.ShowDialog();
            if (openFileDialog.FileName != "")
            {
                saveFileDialog.FileName = openFileDialog.FileName;
                load(openFileDialog.FileName);
            }*/

            panelNameEntry.Visible = false;
            panelNewLoad.Visible = true;
        }
        

        public void NewName(bool state)
        {
            if (state)
            {
                this.textBoxNameEntry.Text = "Default";
                this.AcceptButton = buttonCommit;
                panelNewLoad.Visible = false;
                panelNameEntry.Visible = true;
            }
            else
            {
                this.AcceptButton = buttonNew;
                panelNewLoad.Visible = true;
                panelNameEntry.Visible = false;
            }
        }
        

        /// <summary>
        /// Loads a given file as the kernel, overwriting any currently open kernels.
        /// </summary>
        /// <param name="fileName"></param>
        private void load(string fileName)
        {
            Stream stream = null;
            try
            {
                IFormatter formatter = new BinaryFormatter();
                stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
                userObject = formatter.Deserialize(stream);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
            finally
            {
                if (null != stream)
                    stream.Close();
            }
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            openFileDialog.ShowDialog();
            try
            {
                load(openFileDialog.FileName);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
            this.Close();
        }

        private void buttonNew_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public object Instance
        {
            get { return  userObject; }
        }
        public string InstanceName
        {
            get { return name; }
        }

        private void buttonCommit_Click(object sender, EventArgs e)
        {
            name = textBoxNameEntry.Text;
            this.Close();
        }
    }
}